﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Bussiness;

namespace GUI {
    public partial class frmQuanLySach : Form {
        private List<TacGiaDTO> _tacGiaList;
        private List<TheLoaiDTO> _theLoaiList;

        public frmQuanLySach() {
            InitializeComponent();
        }       

        private void frmQuanLySach_Load(object sender, EventArgs e) {

        }

        private void label1_Click(object sender, EventArgs e) {

        }

        private void btnTim_Click(object sender, EventArgs e) {
            SachService sser = new SachService();
            string maSach = "";
            string tenSach = "";
            int idTacGia = -1;
            int idTheLoai = -1;
            int tonItHon = -1;
            int tonNhieuHon = -1;
            if (chkbxMaSach.Checked)
                maSach = tbxMaSach.Text.Trim();
            if (chkbxTenSach.Checked)
                tenSach = tbxTenSach.Text.Trim();
            if (chkbxTacGia.Checked)
                idTacGia = cbxTacGia.SelectedIndex + 1;
            if (chkbxTheLoai.Checked)
                idTheLoai = cbxTheLoai.SelectedIndex + 1;
            if (chkbxTonItHon.Checked)
                tonItHon = Convert.ToInt32(tonItHon);
            if (chkbxTonNhieuHon.Checked)
                tonNhieuHon = Convert.ToInt32(tonNhieuHon);

            dgvKetQuaTimKiem.DataSource = sser.TimKiem(maSach, tenSach, idTacGia, idTheLoai, tonItHon, tonNhieuHon);
        }

        private void gbxTieuChiTimKiem_Enter(object sender, EventArgs e) {
            //SachService bus = new SachService();
            //dgvKetQuaTimKiem.DataSource = bus.LaySachTB();

            // Lay danh sach tac gia
            TacGiaService tgService = new TacGiaService();
            _tacGiaList = tgService.SelectAllTacGias();

            // Load len Combobox Tac gia
            cbxTacGia.DataSource = this._tacGiaList;
            cbxTacGia.DisplayMember = TacGiaColumn.TenTacGia;
            cbxTacGia.ValueMember = TacGiaColumn.IDTacGia;

            cbxTacGia2.DataSource = this._tacGiaList;
            cbxTacGia2.DisplayMember = TacGiaColumn.TenTacGia;
            cbxTacGia2.ValueMember = TacGiaColumn.IDTacGia;

            //Lay danh sach the loai
            TheLoaiService tlService = new TheLoaiService();
            _theLoaiList = tlService.SelectAllTheLoais();

            // Load len Combobox The loai
            cbxTheLoai.DataSource = this._theLoaiList;
            cbxTheLoai.DisplayMember = TheLoaiColumn.TenTheLoai;
            cbxTheLoai.ValueMember = TheLoaiColumn.MaTheLoai;

            cbxTheLoai2.DataSource = this._theLoaiList;
            cbxTheLoai2.DisplayMember = TheLoaiColumn.TenTheLoai;
            cbxTheLoai2.ValueMember = TheLoaiColumn.MaTheLoai;
        }

        private void dgvKetQuaTimKiem_SelectionChanged(object sender, EventArgs e) {
            txtMaSach.Text = dgvKetQuaTimKiem.CurrentRow.Cells[0].Value.ToString();
            txtTenSach.Text = dgvKetQuaTimKiem.CurrentRow.Cells[1].Value.ToString();
        }

        private void btnThoat_Click(object sender, EventArgs e) {
            Close();
        }

    }
}
